//
//  YDYEssenceTableViewController.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/17.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYEssenceTableViewController.h"
#import "YDYVistorView.h"
#import "YDYTitleBtn.h"
#import "YDYUserAccount.h"
#import "YDYPresentManager.h"
#import "YDYNetWorkTool.h"
#import "YDYHomeTableViewCell.h"
#import "YDYStatusViewModel.h"
@interface YDYEssenceTableViewController ()
/** 判断是不是在展示POPVIEW */
@property (nonatomic, assign,getter=isPresenting) BOOL presenting;
/** 控制转场动画的管理者 */
@property (nonatomic, strong) YDYPresentManager *presentManager;
/** 标题栏上面的按钮 */
@property (nonatomic, weak) UIButton *titleBtn;
/** 网络请求用的管理器 */
@property (nonatomic, strong) YDYNetWorkTool *networkManager;
/** 用户账户信息 */
@property (nonatomic, strong) YDYUserAccount *account;
/** 微博数据viewmodel数组*/
@property (nonatomic, strong) NSArray *statusArray;
@end

@implementation YDYEssenceTableViewController


#pragma mark -
#pragma mark -设置微博数组
-(void)setStatusArray:(NSArray *)statusArray{
    _statusArray = statusArray;
   
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    //1.缓存图片数据
    [self cacheImagesFinished:^{
        //2.刷新界面
        [self.tableView reloadData];
    }];
    
    
}
-(void)cacheImagesFinished:(void(^)())cacheFinished{
    dispatch_group_t group =  dispatch_group_create();
    for (YDYStatusViewModel *viewModel in self.statusArray) {
        if (viewModel.pic_urls.count == 0) continue;
        dispatch_group_enter(group);
        [viewModel cacheImages:^{
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        cacheFinished();
        YDYLog(@"全部微博缓存完成");
    });
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.设置访客视图
    [self.vistorView setVistorViewWithTitle:nil andImageName:nil];
    //2.设置标顶部用户名和按钮
    [self setupTitleView];
    //3.设置监听
    [self setupNotification];
    //4.获取微博数据(异步)
    [self loadStatus];
    
    
    
    
}
-(void)loadStatus{
    [self.networkManager loadStatus:^(NSArray *dataArray, NSError *error) {
        if (dataArray.count ==0)return;
        NSMutableArray *statusArray = [NSMutableArray array];
        for (NSDictionary *dict in dataArray) {
            YDYStatus *status =  [YDYStatus ydyStatusWithDict:dict];
            YDYStatusViewModel *statusViewModel = [YDYStatusViewModel YDYStatusViewModelWithStatus:status];
            [statusArray addObject:statusViewModel];
        }
        self.statusArray = statusArray;
    }];
    

}
-(void)setupNotification{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTitleButtonState) name:YDYPOPViewDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTitleButtonState) name:YDYPOPViewDidDismissedNotification object:nil];
    
}
-(void)setupTitleView{
    YDYUserAccount *account = [YDYUserAccount loadUserAccount];
    
    YDYTitleBtn *btn = [[YDYTitleBtn alloc]init];
    
    [btn setTitle:account.screen_name forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(titleBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    self.titleBtn = btn;
    self.navigationItem.titleView = btn;
    
}

-(void)titleBtnDidClick:(UIButton *)btn{

    //1.弹出菜单视图
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"POPOver" bundle:nil];
    UIViewController *popviewcontroller = storyboard.instantiateInitialViewController;
    //2.修改转场动画
    popviewcontroller.transitioningDelegate = self.presentManager;
    self.presentManager.presentFrame =  CGRectMake(100, 50, 200, 200);
    popviewcontroller.modalPresentationStyle = UIModalPresentationCustom;
    
    [self presentViewController:popviewcontroller animated:YES completion:nil];
    
}
-(void)changeTitleButtonState{
    self.titleBtn.selected = !self.titleBtn.isSelected;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


#pragma mark -
#pragma mark -tableview datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.statusArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YDYHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homecell"];
    cell.status = self.statusArray[indexPath.row];
    return  cell;
}

#pragma mark -
#pragma mark -懒加载
-(YDYPresentManager *)presentManager{
    if (_presentManager == nil) {
        YDYPresentManager *manager = [[YDYPresentManager alloc]init];
        _presentManager = manager;
        
    }
    return  _presentManager;
}
-(YDYNetWorkTool *)networkManager{
    if (_networkManager == nil) {
        _networkManager = [YDYNetWorkTool sharedTool];
    }
    return _networkManager;
}
-(YDYUserAccount *)account{
    if (_account == nil) {
        _account = [YDYUserAccount sharedAccount];
        
    }
    return _account;
}

@end
